package com.gg.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class FetchSize {
    static final String driver_class  = "com.mysql.jdbc.Driver";
    static final String connectionURL = "jdbc:mysql://127.0.0.1:3306/sy?useUnicode=true&characterEncoding=UTF-8";
    static final String userID        = "root";
    static final String userPassword  = "root";
    public void runTest(int fetchSize) {
        Connection  con = null;
        Statement   stmt = null;
        ResultSet   rset = null;
        long startTime =System.currentTimeMillis();
        String  query_string = "SELECT * FROM user";//test有5万条记录
        try {
            Class.forName (driver_class).newInstance();
            con = DriverManager.getConnection(connectionURL, userID, userPassword);
            stmt = con.createStatement();
            stmt.setFetchSize(fetchSize);
            rset = stmt.executeQuery (query_string);
            while (rset.next ()) {
                String a= rset.getString(1);
                System.out.println(a);
                 rset.getString(2);
                 rset.getString(3);
            }
            rset.close();
            stmt.close();
            long endTime =System.currentTimeMillis();
            System.out.println("fetchsize为"+fetchSize+"---消耗的时间:"+(endTime-startTime));
        }  catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        FetchSize fetchSize = new FetchSize();
        fetchSize.runTest(1);
        fetchSize.runTest(2);
        fetchSize.runTest(3);
        fetchSize.runTest(200);
        fetchSize.runTest(500);
        fetchSize.runTest(1000);
    }
}